package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baidu.android.pushservice.PushManager;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Status;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.UserInfo;
import com.neusoft.zqygbc.neusoft_wenda.model.UserModel;

import retrofit2.Call;

public class ChangePwdActivity extends AppCompatActivity {

    private LinearLayout ll_changepwd_return;
    private EditText et_old_pwd,et_new_pwd,et_again_pwd;
    private Button btn_change_pwd;
    SharedPreferences sp;
    private UserModel userModel;
    private Call call;
    private RetrofitCallback<Status> retrofitCallback;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        initView();
        initEvents();
    }

    private void initView(){
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        ll_changepwd_return = (LinearLayout) findViewById(R.id.ll_changepwd_return);
        et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
        et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
        et_again_pwd = (EditText) findViewById(R.id.et_again_pwd);
        btn_change_pwd = (Button) findViewById(R.id.btn_change_pwd);
    }

    private void initEvents(){
        ll_changepwd_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btn_change_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_new_pwd.getText().toString().equals(et_again_pwd.getText().toString()) ){
                    initData();
                }else {
                    Toast.makeText(ChangePwdActivity.this, "两次新密码不一样", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initData() {
        userModel=new UserModel();
        call=userModel.updatepwd(sp.getString("user_id", ""),
                et_old_pwd.getText().toString(),
                et_new_pwd.getText().toString());
        retrofitCallback=new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if(status.getCode().equals("1")){
                    Toast.makeText(ChangePwdActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.remove("user_id");
                    editor.commit();
                    //百度推送解除绑定设备
                    PushManager.stopWork(getApplicationContext());
                    Intent intent = new Intent(ChangePwdActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(ChangePwdActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        userModel.bindCallback(call,retrofitCallback);


    }
}
